package DBD::Yahoo::dr;

use strict;
use base qw(DBD::_::dr);
use vars qw($VERSION $imp_data_size);

use Carp qw(carp croak);
use DBI;
use Yahoo::Search;

$VERSION       = "0.01";
$imp_data_size = 0;

my @valid_yahoo_opts =
  qw(AllowAdult AllowSimilar AppId AutoContinue City Color Context Count Country Debug Language Lat License Location Long Mode PostalCode PreRequestCallback Radius Sort Start State Street Type);

# ----------------------------------------------------------------------
# connect($dsn, $user, $pass, \%attrs);
#
# Method called when an external process does:
#
#   my %opts = ("AllowAdult" => 0, "AllowSimilar" => 0, "Count" => 10);
#   my $dbh = DBI->connect("dbi:Yahoo:", $AppId, undef, \%opts);
#
# Username must be the Yahoo AppId, password is ignored and can be
# anything, and valid options in the %attr hash are passed to Yahoo::Search.
# ----------------------------------------------------------------------
sub connect
{
  my ($drh, $dbname, $user, $pass, $attr) = @_;
  my ($dbh, $yahoo, %yahoo_opts);

  $attr->{'AppId'} ||= $user            || '';
  $user            ||= $attr->{'AppId'} || '';
  $pass            ||= '';

  $dbh = DBI::_new_dbh(
    $drh,
    {
      'Name'         => $dbname,
      'USER'         => $user,
      'CURRENT_USER' => $user,
      'Password'     => $pass,
    }
  );

  # Get options from %attr.  These will be passed to $yahoo->Request
  for my $yahoo_opt (@valid_yahoo_opts)
  {
    if (defined $attr->{$yahoo_opt})
    {
      $yahoo_opts{$yahoo_opt} = delete $attr->{$yahoo_opt};
    }
  }

  # Create a Yahoo::Search instance, and store it.
  # We can reuse this for multiple queries.
  $yahoo = Yahoo::Search->new(%yahoo_opts) || die $!;

  $dbh->STORE('driver_yahoo'      => $yahoo);
  $dbh->STORE('driver_yahoo_opts' => \%yahoo_opts);

  return $dbh;
}

sub disconnect_all { 1 }

sub data_sources { return "Yahoo" }

sub DESTROY { undef }

1;
